package com.example.yrdev.ordering_system.Dialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;

import com.example.yrdev.ordering_system.Model.Good;
import com.example.yrdev.ordering_system.R;
import com.example.yrdev.ordering_system.Utils.GlobalFunction;

import java.util.ArrayList;
import java.util.Locale;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class ShoppingCartDialog extends Dialog {
     private Context context;
    @InjectView(R.id.total_price)TextView total_price;
    private ArrayList<Good> shoppingList;

    private    shopCartListener shopCartListener;

 public    interface shopCartListener{

        void  pay(ArrayList<Good> shoppingList);

    }

   public ShoppingCartDialog(Context context){

        super(context);
        this.context=context;
       shoppingList=new ArrayList<>();
        requestWindowFeature(Window.FEATURE_NO_TITLE);


    }

    public void setShoppingList(ArrayList<Good> shoppingList) {
        this.shoppingList = shoppingList;
    }

    public ArrayList<Good> getShoppingList() {
        return this.shoppingList;
    }

    public void setShopCartListener(ShoppingCartDialog.shopCartListener shopCartListener) {
        this.shopCartListener = shopCartListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // setContentView(R.layout.shopping_cart_dialog_layout_copy);


        ButterKnife.inject(this);
        getWindow().setLayout(  WindowManager.LayoutParams.MATCH_PARENT,   WindowManager.LayoutParams.WRAP_CONTENT);


        initView();

        checkCurrentPrice();
    }
    public void clearShoppinglist(){
        shoppingList.clear();
      //  adapter.notifyDataSetChanged();
    }
    ListAdapter adapter;

   // list_adapter adapter;

  private void initView(){

      adapter=new ListAdapter(context);

        Button conPurch=(Button)findViewById(R.id.continue_purchase);
        Button goPay=(Button)findViewById(R.id.pay);

        conPurch.setOnClickListener(dialogClickListener());
        goPay.setOnClickListener(dialogClickListener());

        SwipeMenuListView menuListView=(SwipeMenuListView)findViewById(R.id.swipe_list);
//        adapter =new list_adapter(context,item_selected);
//        adapter.setShowTotalPrice(true);

    menuListView.setAdapter(adapter);
        SwipeMenuCreator creator = new SwipeMenuCreator() {

            @Override
            public void create(SwipeMenu menu) {


                SwipeMenuItem deleteItem = new SwipeMenuItem(
                        context);
                // set item background
                deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
                        0x3F, 0x25)));
                // set item width
                deleteItem.setWidth(90);
                // set a icon
                deleteItem.setTitle("删除");
                deleteItem.setTitleSize(18);
                deleteItem.setTitleColor(Color.WHITE);
                // add to menu
                menu.addMenuItem(deleteItem);
            }
        };

        menuListView.setMenuCreator(creator);


        menuListView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {

                GlobalFunction.printTextByTag("click:"+ String.valueOf(index)+":"+ String.valueOf(position));
           //     item_selected.remove(position);

         //       adapter.notifyDataSetChanged();

//                if (item_selected.size()==0)
//                    dismiss();

                checkCurrentPrice();

                return false;
            }
        });


    }

   private View.OnClickListener dialogClickListener(){

        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();

                //                shoppingCartDialog.dismiss();
//
                if (v.getId()==R.id.pay){

                    if (shopCartListener!=null){

                        shopCartListener.pay(shoppingList);

                    }
                }
            }
        };


    }
    public  void addNewShoppingGood(Good good){
        boolean exists=false;
        for (Good temp:shoppingList){
            if (temp.getId().equals(good.getId())){
                exists=true;

                temp.setPurchaseCount(temp.getPurchaseCount()+good.getPurchaseCount());
                break;

            }
        }


        if (!exists){
            shoppingList.add(good);

        }
    }
   private   void checkCurrentPrice(){
        double price=0;

        for (int i=0;i<shoppingList.size();i++){
            Good goods=shoppingList.get(i);
            price=price+ Double.valueOf(goods.getTotalPrice());


        }

        total_price.setText( String.format(Locale.getDefault(),"%.2f",price)+"元");

    }

    @Override
    public void show() {
        super.show();
        adapter.notifyDataSetChanged();
//        adapter.setArrayList(item_selected);
//        adapter.notifyDataSetChanged();
        checkCurrentPrice();
    }


    private  class ListAdapter extends BaseAdapter {
        LayoutInflater inflater;

        ListAdapter(Context context){
           inflater= LayoutInflater.from(context);

        }
        @Override
        public Good getItem(int position) {
            return shoppingList.get(position);

        }

        @Override
        public int getCount() {
            return shoppingList.size();

        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if (convertView==null){

               // convertView=inflater.inflate(R.layout.shopping_row,null);

            }
            Good respGood=getItem(position);

            TextView goodCode=(TextView)convertView.findViewById(R.id.goods_code);
            TextView goodName=(TextView)convertView.findViewById(R.id.goods_name);
            TextView goodAmount=(TextView)convertView.findViewById(R.id.goods_amount);
            TextView totPrice=(TextView)convertView.findViewById(R.id.goods_retail_price);

            goodCode.setText(respGood.getId());
            goodName.setText(respGood.getName());

            goodAmount.setText(String.valueOf(respGood.getPurchaseCount()));
            totPrice.setText(respGood.getTotalPrice());
            return convertView;

        }
    }
}